Skip to content

spacemit: fuse H32 GDN state transfers and write back in place - #97

Merged
alex-spacemit merged 1 commit into
spacemit-com:mtmd-backendfrom
co-seven:pr/nomtp-gdn-20261203
Dec 4, 2026
Merged

alex-spacemit merged 1 commit into
spacemit-com:mtmd-backendfrom
co-seven:pr/nomtp-gdn-20261203

Conversation

@co-seven

@co-seven co-seven commented Dec 3, 2026

Copy link
Copy Markdown
Collaborator

Problem and behavior

Performance optimization following #88. Extend single-token GDN state-transfer fusion from H16 to H32 and avoid materializing/copying the large state tail when it can be written safely to the persistent cache row in place. This includes the direct writeback change, not only the original H32 dispatch extension.

Preserve full state materialization whenever an exported tensor, output view or another consumer needs it. In particular, the planner uses scheduler export information rather than assuming that absence of a local consumer means absence of a later backend consumer.

Change and design

  • ggml-spacemit.cpp: H32 matching, graph output/root and export checks, lifetime dependencies, and the private-state writeback flag. Missing scheduler information, callback slices and unsafe aliases keep the conservative path.
  • ime.cpp: pass the validated writeback decision to the RVV executor.
  • rvv_kernels.cpp/.h: accept direct writeback, preserve materialization when required, and avoid same-address memcpy in the narrow-vector fallback.

The original upstream MTP multi-snapshot/transposed state path remains present. The added direct-writeback match is restricted to the supported single-token F32 state [128,128,32].

Scope is limited to ggml/src/ggml-spacemit/. Impact classification: execution-path change. No changes to the retired CPU backend, model code, public API or test sources. Local validation fixtures are excluded. This independently based module commit preserves the target branch's common MTP graph/rollback paths; experimental noMTP-wide reversions are not submitted. No new external dependency is added.

Validation

Base: mtmd-backend at 3f5ce5fd84f4dc6e7e621642cfef0143d49e7c24. Candidate: ee469bd010f77014e3276a083c7e4828dfe6cd47; source tree 858480f8571ea48403233132b634a2ac2a52a54f.

The exact submitted production source passed a RISC-V Release cross-build of ggml-spacemit using the SpacemiT v1.1.2 toolchain (GCC 14.3), cmake/riscv64-spacemit-linux-gnu-gcc.cmake, GGML_SPACEMIT=ON, GGML_RV_ZBA=ON, GGML_OPENMP=OFF, Spert 0.6.3 and SpacemiT ORT 2.0.6. Build target: cmake --build <build> --target ggml-spacemit --parallel 24. Backend DSO SHA-256: 78c4cca917e11ccbb0750ab53ce82e515e405fb5037941a3b0eff2c02b8b40fc. Source hashes were checked before/after the build and against the committed files.

The final publication source passed 9/9 host planner cases, re-extracted from this exact source: private H32 state, H16, exported GDN, exported gather, local output view, external output view, extra reader, eval callback and replanning. Only private H32 enabled tail elision. These tests check planner decisions, not numerical execution of RVV.

An earlier aggregate K3 build with direct writeback passed four numerical checks: private state, cross-backend tail reads, exported gather, and 16-token/16-snapshot state. A debugger verified that the real model state row was read and written at the same persistent address. The final narrow-vector same-address copy guard was added after that board run; no VLEN-256 numerical result is claimed.

Check Status Evidence / scope
Exact-source RISC-V backend build PASS Configuration and DSO identity above
Diff / scope / formatting checks PASS Backend-only; no test changes or new environment controls; git diff --check, UTF-8/LF and added-line whitespace
Final-source planner guards PASS 9/9 host cases; only private H32 skips tail materialization
Numerical execution and actual in-place write PASS (historical aggregate) Four checks plus real-address debugger verification
H32 model output equality PASS (historical) All 128 IDs match
Stable H32 / isolated writeback speedup NOT ESTABLISHED H32 startup drift exceeds mean gain; writeback has no paired same-board control
Final-head board / additional platform coverage NOT RUN Historical target results are carried forward; no new final-head K3 or K1 run

Performance

Two experiments answer different questions:

Experiment Baseline decode tok/s Candidate decode tok/s Samples per arm Interpretation
H32 fusion ablation, Qwen3.6-35B-A3B Q4_0, K3-175 8.286751 8.527744 6 +2.91% mean; startup variation was 5.17%, so stable gain is inconclusive
Aggregate with H32 direct writeback, K3-179 Not measured on the same board 9.228469 One candidate sample Absolute observation only; no isolated writeback speedup

H32 ablation: fixed 10,240 prompt IDs, 128 generated tokens, MTP off, F16 KV, FA on, -c 12288 -b 2048 -ub 512 -t 8, eight workers; two startups per arm. All output IDs matched. This experiment predates direct writeback. Run record: qwen36-h32-kcache-ab-20261202.

Direct-writeback observation: 10,240 input tokens, 128 generated tokens, MTP off, -c 32768 -b 2048 -ub 2048 -t 8, eight workers; 117.546 prefill tok/s and 9.228 decode tok/s. Run label B179. The aggregate also contained other noMTP optimizations; it is not a standalone GDN A/B. Cross-board values must not be used to attribute a speedup to writeback.

Risks and limits

The historical numerical/model runs exercised an aggregate package. The final standalone head, narrow-vector numerical fallback and wider MTP rollback workloads need board validation. Host guard tests do not replace those checks. The cross-split materialization concern from #88 is addressed by explicit export and output-root checks.

The historical module comparisons used the common noMTP experimental baseline on top of 3f5ce5fd; this PR instead preserves upstream common MTP behavior. Historical results are not claimed as new measurements of the final independent head. Raw build/test/benchmark archives are retained privately and are not publicly attached; the cases, results, artifact identities and limits above are the reviewable record here. Exact-input model replay is not provided by these summaries. Required final-head target evidence remains pending; this submission requests review, not a waiver or a claim of merge readiness. No maintainer exception has been granted. CI status is separate from the local results above.

AI assistance and contributor responsibility

Codex assisted with source preparation, static review, local build/check execution, historical evidence analysis and this PR description. The contributor discussed the module design and explicitly requested publication. No unrecorded personal review, maintainer approval or target execution is asserted. The contributor remains responsible for reviewing the submitted code and claims and addressing review feedback.

Copy link
Copy Markdown
Collaborator

Please extend the output-root check to the GET_ROWS gather before marking it eligible for elision. The full-graph check currently protects the GDN output but not the gather. A terminal OUTPUT view rooted at the gather can be outside the SpacemiT split with no compute consumer. In that case the local matcher and split-export check can miss it, and the direct path can leave its output unwritten.

Please add a cross-split terminal gather-view regression that compares the exposed pre-update gather and the post-update persistent row separately. For the performance claim, please compare baseline, H32 with the state tail materialized, and H32 direct writeback on the same K3 setup. The reported +2.91% is smaller than the reported startup variation, so it doesn't isolate a stable gain yet.

@co-seven

co-seven commented Dec 4, 2026

Copy link
Copy Markdown
Collaborator Author

The terminal gather-view case is already protected when the output is marked with ggml_set_output in this base. That function propagates GGML_TENSOR_FLAG_OUTPUT through view_src to the storage root. The gather itself is therefore marked as an output, and gdn_state_gather_match rejects its elision even when the terminal view is outside the SpacemiT split.

I extended the host planner fixture with exactly that construction: a terminal output view rooted at the gather, appended outside the local split, with no compute consumer. The planner keeps the gather materialized (cache_only=0, skip_gather=0); all ten planner controls pass. This is host planning evidence, not the requested target numerical comparison of the old gather and updated cache row.

Directly setting only the view's output flag, without calling ggml_set_output, does bypass the root-flag propagation and is a different case. Please share the output-marking sequence if that is how your reproducer is constructed. No code change has been made for the standard-API scenario above.

AI assistance: Codex assisted with tracing the source and running the host planner fixture; this reply was explicitly requested by the contributor.

@alex-spacemit
alex-spacemit merged commit c9a0371 into spacemit-com:mtmd-backend Dec 4, 2026
13 of 20 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants